package xyz.larkin.common.api.impl;

import com.google.gson.Gson;

/**
 * Created by liujin on 2019/1/7.
 *
 * @description TODO
 */
public class JsonUtils {
    public static String objectToJson(Object obj) {
        Gson gson = new Gson();
        return gson.toJson(obj);
    }

    public static Object jsonToObject(String json, String className) {
        Gson gson = new Gson();
        try {
            return gson.fromJson(json, Class.forName(className));
        } catch (ClassNotFoundException e) {
            return null;
        }
    }

    public static <T> T jsonToObject(String json, Class<T> classType) {
        Gson gson = new Gson();

        return gson.fromJson(json, classType);

    }
}
